#include "PlatformProperty.h"

#define MSG_VEHICLE_SPEED_SAMPLE 100
bool hasVehicleSpeedSampleMsg = false;

SYSTEM_CONTINUOUS_PROPERTY(PERF_VEHICLE_SPEED, float) {
    builder.sampleRate(1.0f, 10.0f)
            .area(VehicleArea::GLOBAL)
            .initValue([]() {
                return 0.0f;
            })
            .immutableStatus(PropertyStatus::Active)
            .withValueBySignals([this]([[maybe_unused]] const float &last) {
                float value = SIGNAL.getVehSpdLgtA();
                VHALOG.d("PERF_VEHICLE_SPEED value[%d] hasVehicleSpeedSampleMsg:%d", value,hasVehicleSpeedSampleMsg);
                if(!hasVehicleSpeedSampleMsg){
                    hasVehicleSpeedSampleMsg = true;
                    sendMsg(MSG_VEHICLE_SPEED_SAMPLE, 1000);
                }
                return value;
            }, SignalRX::VehSpdLgtA)
            .onMsgEvent(MSG_VEHICLE_SPEED_SAMPLE, [this]() {
                VHALOG.d("MSG_VEHICLE_SPEED_SAMPLE [%d]", hasVehicleSpeedSampleMsg);
                setPropertyValue(static_cast<int32_t>(ECarXVehicleProperty::VEHICLE_SPEED_PER_SECOND),
                    toInt(VehicleArea::GLOBAL), SIGNAL.getVehSpdLgtA());
                hasVehicleSpeedSampleMsg = false;
            });;
}
